package com.wwwarehouse.carddesk.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.adapter.MessageDetailsAdapter;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.carddesk.constant.MessageRouterConstant;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.LogUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;



/**
 * 信息框架
 * minifly
 * 0924: 10:22
 */

public class MessageDetailsFragment extends BaseFragment  implements View.OnClickListener, EasyPopupWindow.ChildClickListener{



    public final static String KEY_LIMIT = "limit";
    public final static String KEY_PAGENUM = "pagenum";
    public final static String KEY_ORDERSORT = "ordersort";
    public final static String KEY_SEARCHTEXT = "searchtext";
    public final static String KEY_TAG_LIST = "taglist";

    private View mRootView;
    private StateLayout mStateLayout;
    private GridView mGridView;
    private MessageDetailsAdapter mAdapter;

    private int limitNum = 10;//默认是三十个卡片，三页的样子
    private String currentOrderSort = "";
    private String searchText; //搜索关键字
    private int pageNum = 1; //页数


//    private static final int REQUEST_GET_MESSAGE_INFO = 10003;
    private final static int REQUEST_GET_STABLE_CARD = 10004;
    private CardDeskMessageStableCardBean stableCardBean;
    private List<CardDeskMessageStableCardBean.TasksBean> stableList;
    private LinkedList<String> taskList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_message_details, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mStateLayout = findView(mRootView, R.id.message_gridview_layout);
        mGridView = findView(mRootView, R.id.message_gridview);

        Bundle bundle = getArguments();

        LogUtils.showErrLog("bundle" + bundle.toString());

        if (bundle != null) {

            limitNum = bundle.getInt(KEY_LIMIT);
            pageNum = bundle.getInt(KEY_PAGENUM);
            currentOrderSort = bundle.getString(KEY_ORDERSORT);
            searchText = bundle.getString(KEY_SEARCHTEXT);
            taskList = (LinkedList<String>) bundle.getSerializable(KEY_TAG_LIST);
        }

        stableList = new LinkedList<>();

    }


    private void initData() {
        getStableCard();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void getChildView(int layoutId, View view) {
        Button favorite = (Button) view.findViewById(R.id.btn_favorite);
        Button cancel = (Button) view.findViewById(R.id.btn_cancel);

        favorite.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }
    /**
     * 获取固定卡片的值
     */
    private void getStableCard(){
        Map dataMap = new HashMap();
        dataMap.put("actionType","I");
        dataMap.put("searchText",searchText);

        if(taskList ==null || taskList.size() == 0){
            //这个时候给null给后台
            dataMap.put("taskTags",null);
        }else{
            dataMap.put("taskTags",taskList);
        }

        dataMap.put("buId",null);
        dataMap.put("limit","" + limitNum);
        dataMap.put("page","" + pageNum);
        dataMap.put("orderSort","" + currentOrderSort);
        dataMap.put("orderType","CREATE_TIME");

        LogUtils.showErrLog("接口： "+CardDeskConstant.URL_GET_FUNCTION_INFO+" 发送请求" + dataMap.toString());
        NoHttpUtils.httpPost(CardDeskConstant.URL_GET_FUNCTION_INFO, dataMap, mOnResponseListener, REQUEST_GET_STABLE_CARD);

    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_STABLE_CARD:
                    if ("0".equals(commonClass.getCode())) {
                        LogUtils.showErrLog("" + commonClass.getData().toString());
                        stableCardBean = JSON.parseObject(commonClass.getData().toString(),CardDeskMessageStableCardBean.class);
                        stableList = stableCardBean.getTasks();

                        Rect rect = new Rect();
                        mStateLayout.getDrawingRect(rect);
                        int itemHeight2 =(rect.height() -  ConvertUtils.dip2px(mActivity,40)) / 5;
                        mAdapter = new MessageDetailsAdapter(mActivity, stableList, itemHeight2);
                        mGridView.setAdapter(mAdapter);

                        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                if(Common.getInstance().isNotFastClick()){
                                    android.support.v4.app.Fragment fragment = MessageRouterConstant.goPath(stableList.get(position).getTaskType());
                                    if(fragment!=null){
                                        MessageRouterConstant.dealData(stableCardBean,stableList.get(position));
                                        MessageRouterConstant.setBundle(fragment,stableList.get(position));
                                        pushFragment(fragment);
                                    }
                                }
                            }
                        });

                    }else{
                        //其他的同步线程没有处理的code
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(true);
                                getStableCard();
                            }
                        });
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(true);

                    getStableCard();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };
}
